home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / util / wdwrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-29  |  3.0 KB  |  141 lines

  1. /* wdwrite.c - write key/value to defaults database
  2.  *
  3.  *  WindowMaker window manager
  4.  * 
  5.  *  Copyright (c) 1997, 1998 Alfredo K. Kojima
  6.  * 
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
  20.  *  USA.
  21.  */
  22.  
  23. #define PROG_VERSION    "wdwrite (Window Maker) 0.2"
  24.  
  25.  
  26. /*
  27.  * WindowMaker defaults DB writer
  28.  */
  29.  
  30.  
  31. #include "../src/wconfig.h"
  32.  
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <unistd.h>
  37.  
  38. #include <proplist.h>
  39. #include <pwd.h>
  40.  
  41.  
  42. char *ProgName;
  43.  
  44. char*
  45. gethomedir()
  46. {
  47.     char *home = getenv("HOME");
  48.     struct passwd *user;
  49.  
  50.     if (home)
  51.       return home;
  52.     
  53.     user = getpwuid(getuid());
  54.     if (!user) {
  55.     perror(ProgName);
  56.         return "/";
  57.     }
  58.     if (!user->pw_dir) {
  59.         return "/";
  60.     } else {
  61.         return user->pw_dir;
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. void wAbort()
  68. {
  69.     exit(0);
  70. }
  71.  
  72. void help()
  73. {
  74.     printf("Syntax:\n%s [OPTIONS] <domain> <option> <value>\n", ProgName);
  75.     puts("");
  76.     puts("  --help        display this help message");
  77.     puts("  --version        output version information and exit");
  78.     exit(1);
  79. }
  80.  
  81.  
  82. int main(int argc, char **argv)
  83. {
  84.     char path[256];
  85.     proplist_t dom, key, value, dict;
  86.     char *gsdir;
  87.     int i;
  88.     
  89.     ProgName = argv[0];
  90.  
  91.     for (i = 1; i < argc; i++) {
  92.     if (strcmp("--help", argv[i])==0) {
  93.         help();
  94.         exit(0);
  95.     } else if (strcmp("--version", argv[i])==0) {
  96.         puts(PROG_VERSION);
  97.         exit(0);
  98.     }
  99.     }
  100.  
  101.     if (argc<4) {
  102.     printf("%s: invalid argument format\n", ProgName);
  103.     printf("Try '%s --help' for more information\n", ProgName);
  104.     exit(1);
  105.     }
  106.     
  107.     dom = PLMakeString(argv[1]);
  108.     key = PLMakeString(argv[2]);
  109.     value = PLGetProplistWithDescription(argv[3]);
  110.     if (!value) {
  111.     printf("%s:syntax error in value \"%s\"", ProgName, argv[3]);
  112.     exit(1);
  113.     }
  114.     gsdir = getenv("GNUSTEP_USER_ROOT");
  115.     if (gsdir) {
  116.     strcpy(path, gsdir);
  117.     } else {
  118.     strcpy(path, gethomedir());
  119.     strcat(path, "/GNUstep");
  120.     }
  121.     strcat(path, "/");
  122.     strcat(path, DEFAULTS_DIR);
  123.     strcat(path, "/");
  124.     strcat(path, argv[1]);
  125.  
  126.     dict = PLGetProplistWithPath(path);
  127.     if (!dict) {
  128.     dict = PLMakeDictionaryFromEntries(key, value, NULL);
  129.     PLSetFilename(dict, PLMakeString(path));
  130.     } else {
  131.     PLRemoveDictionaryEntry(dict, key);
  132.     PLInsertDictionaryEntry(dict, key, value);
  133.     }
  134.     
  135.     PLSave(dict, YES);
  136.     
  137.     return 0;
  138. }
  139.  
  140.  
  141.